package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.SellSonBiz;
import com.wf.ew.sell.model.SellSon;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-12-08 23:22:41
 * @version ：1.0
 */
@Repository
public class SellSonBizImpl extends BaseDao implements SellSonBiz {

	@Override
	public String list(PageResult<T> page, SellSon sellSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
		StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.companyId");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.price,s.totalPrice");
		sql.append(",s.liWeight,s.actualWeight,s.liTotalWeight,s.actualTotalWeight,s.model,s.locality");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
		sql.append(" where s.del=0 and cmy.del=0 and s.sellId=:sellId ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and s.companyId=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		map.put("sellId", sellSon.getSellId());
    	if(Utils.isNotEmpty(sellSon)){
//			if(Utils.isNotEmpty(sellSon.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", sellSon.getName());
//			}
			if(Utils.isNotEmpty(sellSon.getSort())&&Utils.isNotEmpty(sellSon.getOrder())){
				sql.append(" order by "+sellSon.getSort()+" "+sellSon.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<SellSon> list(SellSon sellSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
        StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.companyId");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.price,s.totalPrice");
		sql.append(",s.liWeight,s.actualWeight,s.liTotalWeight,s.actualTotalWeight,s.model,s.locality");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.sellId=:sellId ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
		map.put("sellId", sellSon.getSellId());
		if(Utils.isNotEmpty(sellSon)){
//			if(Utils.isNotEmpty(sellSon.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", sellSon.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Double calcTotalPrice(SellSon sellSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
        StringBuffer sql = new StringBuffer("select IFNULL(sum(s.totalPrice),0) totalPrice ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.sellId=:sellId ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
		}
		map.put("sellId", sellSon.getSellId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(),map,Double.class);
	}

	@Override
	public SellSon getById(SellSon sellSon) {
		RowMapper<SellSon> rm = new BeanPropertyRowMapper<SellSon>(SellSon.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select s.id,s.sellId,s.companyId");
        sql.append(",s.warehouseName,s.productName,s.specification,s.lengths");
		sql.append(",s.negative,s.num,s.price,s.totalPrice");
		sql.append(",s.liWeight,s.actualWeight,s.liTotalWeight,s.actualTotalWeight,s.model,s.locality");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell_son s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.id=:id ");
		map.put("id", sellSon.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(SellSon sellSon, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_sell_son (");
        sql.append("sellId,companyId,warehouseName,productName");
		sql.append(",specification,lengths,negative,num,price");
		sql.append(",liTotalWeight,actualTotalWeight");
		sql.append(",totalPrice,liWeight,actualWeight");
		sql.append(",model,locality,createUser) values ( ");
        sql.append(":sellId,:companyId,:warehouseName,:productName");
		sql.append(",:specification,:lengths,:negative,:num,:price");
		sql.append(",:liTotalWeight,:actualTotalWeight");
		sql.append(",:totalPrice,:liWeight,:actualWeight");
		sql.append(",:model,:locality,:createUser)");
        map.put("sellId", sellSon.getSellId()==null?null:sellSon.getSellId()+"");
        map.put("companyId", loginUser.getCompanyId());
        map.put("warehouseName", sellSon.getWarehouseName());
        map.put("productName", sellSon.getProductName());
        map.put("specification", sellSon.getSpecification());
        map.put("lengths", sellSon.getLengths());
        map.put("negative", sellSon.getNegative());
        map.put("num", sellSon.getNum()+"");
        map.put("price", sellSon.getPrice());
        map.put("liTotalWeight", sellSon.getLiTotalWeight());
        map.put("actualTotalWeight", sellSon.getActualTotalWeight());
        map.put("totalPrice", sellSon.getTotalPrice());
        map.put("liWeight", sellSon.getLiWeight());
        map.put("actualWeight", sellSon.getActualWeight());
        map.put("model", sellSon.getModel());
        map.put("locality", sellSon.getLocality());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(SellSon sellSon, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_sell_son set ");
        sql.append("sellId=:sellId,warehouseName=:warehouseName,productName=:productName ");
		sql.append(",specification=:specification,lengths=:lengths,negative=:negative,num=:num ");
		sql.append(",liTotalWeight=:liTotalWeight,actualTotalWeight=:actualTotalWeight ");
		sql.append(",price=:price,totalPrice=:totalPrice ");
		sql.append(",liWeight=:liWeight,actualWeight=:actualWeight,model=:model ");
		sql.append(",locality=:locality,updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("sellId", sellSon.getSellId()==null?null:sellSon.getSellId()+"");
        map.put("warehouseName", sellSon.getWarehouseName());
        map.put("productName", sellSon.getProductName());
        map.put("specification", sellSon.getSpecification());
        map.put("lengths", sellSon.getLengths());
        map.put("negative", sellSon.getNegative());
        map.put("num", sellSon.getNum()+"");
        map.put("liTotalWeight", sellSon.getLiTotalWeight());
        map.put("actualTotalWeight", sellSon.getActualTotalWeight());
        map.put("price", sellSon.getPrice());
        map.put("totalPrice", sellSon.getTotalPrice());
        map.put("liWeight", sellSon.getLiWeight());
        map.put("actualWeight", sellSon.getActualWeight());
        map.put("model", sellSon.getModel());
        map.put("locality", sellSon.getLocality());
		map.put("updateUser", loginUser.getId());
		map.put("id", sellSon.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(SellSon sellSon, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(sellSon.getIds().length()>0){
			for(String ID : sellSon.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_sell_son set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
